package pe.gob.foncodes.ssa.web.controller;

import java.util.List;
import java.util.ArrayList;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.primefaces.context.RequestContext;
import pe.gob.foncodes.ssa.bs.administracion.controller.GenericMaintenanceController;
import pe.gob.foncodes.ssa.bs.common.util.Constante;
import pe.gob.foncodes.ssa.bs.seguridad.domain.ControlAcceso;
import pe.gob.foncodes.ssa.bs.seguridad.domain.General;

public class GeneralesController extends GenericMaintenanceController {

    protected static Logger log = Logger.getLogger(ConexionController.class);
    private List<General> lstGeneral = new ArrayList<General>();
    private List<String> lstGeneralDominio = new ArrayList<String>();
    private General selectedGeneral = new General();
    private String selectedGeneralDominio = "";
    
    private String txtBusDominio;
    private boolean esNuevo;

    @Override
    public void init() {
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        selectedGeneral = null;
        esNuevo = false;
        lstGeneral = new ArrayList<General>();
        lstGeneralDominio = new ArrayList<String>();
        setBeanList(lstGeneral);
    }
    
    public void searchGeneral() throws Exception {
        if (this.txtBusDominio == null) {
            this.txtBusDominio = "";
        }
        String stxtBusDominio = "%" + this.txtBusDominio.toUpperCase() + "%";
        lstGeneral = getService().findByNamedQuery("General.findByTxtDominio", new Object[]{stxtBusDominio});
        setBeanList(lstGeneral);
        selectedGeneral = null;
    }

    public void searchDominio() throws Exception {
        if (this.txtBusDominio == null) {
            this.txtBusDominio = "";
        }
        String stxtBusDominio = "%" + this.txtBusDominio.toUpperCase() + "%";
        lstGeneralDominio = getService().findByNamedQuery("General.findOnlyTxtDominio", new Object[]{stxtBusDominio});
        setBeanList(lstGeneralDominio);
        selectedGeneral = null;
    }
    
    public void limpiarBusquedaDominioSeleccion() throws Exception {
         this.txtBusDominio = "";
         lstGeneralDominio = new ArrayList<String>();
    }
    
    @Override
    public void clean(ActionEvent event) throws Exception {
        this.txtBusDominio = "";
        lstGeneral = new ArrayList<General>();
        setBeanList(lstGeneral);
        selectedGeneral = null;

    }

    public void nuevo() {
        RequestContext context = RequestContext.getCurrentInstance();
        esNuevo = true;
        setBean(new General());
        context.execute("editarContenidoGenerales.show()");
    }

    public void verDetallesGenerales() {
        RequestContext context = RequestContext.getCurrentInstance();
        if (this.selectedGeneral == null) {
            setMessageError("Debe seleccionar un registro");
            esNuevo = false;
        } else {
            esNuevo = false;
            setBean(selectedGeneral);
            context.execute("editarContenidoGenerales.show()");
        }
    }

    public void validarDesactivar() {
        RequestContext context = RequestContext.getCurrentInstance();
        if (this.selectedGeneral == null) {
            setMessageError("Debe seleccionar un registro");
            esNuevo = false;
        } else {
            esNuevo = false;
            ControlAcceso control = ((ControlAcceso) getSpringBean(Constante.SESSION_ACCESO));
            selectedGeneral.setNIdCtrlAccesoWeb(control.getId());
            setBean(selectedGeneral);
            context.execute("dlgConfirmacion.show()");
        }
    }
    
    public void saveGeneral(ActionEvent event) throws Exception {
        RequestContext context = RequestContext.getCurrentInstance();
        ControlAcceso control = ((ControlAcceso) getSpringBean(Constante.SESSION_ACCESO));
        General general = (General) getBean();
        general.setNIdCtrlAccesoWeb(control.getId());
        if (general.getTxtParametro() != null) {
            general.setTxtParametro(general.getTxtParametro().toUpperCase());
        }
        setBean(general);
        try {
            if (esNuevo) {
                if (validate()) {
                    super.beforeSave(event);
                    super.doSave(event);
                    afterSave(event);
                    context.execute("editarContenidoGenerales.hide()");
                }
            } else {
                if (validate()) {
                    super.beforeUpdate(event);
                    super.doUpdate(event);
                    afterUpdate(event);
                    context.execute("editarContenidoGenerales.hide()");
                }
            }
        } catch (Exception e) {
            log.error("ERROR EN saveGeneral: "+e);
        }
    }
    
    @Override
    public void afterSave(ActionEvent event) throws Exception {
        esNuevo = false;
        setMessageSuccess("Se creo el parámetro general satisfactoriamente.");
        log.info("End of Transaction: 'Save'");
    }
    
    @Override
    public void afterUpdate(ActionEvent event) throws Exception {
        setMessageSuccess("El registro se actualizó satisfactoriamente.");
        log.info("End of Transaction: 'Update'");
    }
    
    public void desactivarGeneral(ActionEvent event) throws Exception {
        try {
            if (validate()) {
                super.beforeDelete(event);
                super.doDelete(event);
                afterDelete(event);
            }
        } catch (Exception e) {
            log.error("ERROR EN desactivarGeneral: "+e);
        }
    }
    
    @Override
    public void afterDelete(ActionEvent event) throws Exception {
        setMessageSuccess("El registro se desactivó satisfactoriamente.");
        log.info("End of Transaction: 'Delete'");
    }
    
    public void actualizarDominioSeleccion(String dominioseleccionado) throws Exception{
        this.selectedGeneralDominio = dominioseleccionado;
        General general = (General) getBean();
        if(this.selectedGeneralDominio != null){
            general.setTxtDominio(this.selectedGeneralDominio);
        }else{
            log.info("El dominio seleccionado es null");
        }
    }

    @Override
    public boolean validate() throws Exception {
        boolean success = true;
        return success;
    }

    /**
     * @return the lstGeneral
     */
    public List<General> getLstGeneral() {
        return lstGeneral;
    }

    /**
     * @param lstGeneral the lstGeneral to set
     */
    public void setLstGeneral(List<General> lstGeneral) {
        this.lstGeneral = lstGeneral;
    }

    public General getSelectedGeneral() {
        return selectedGeneral;
    }

    public void setSelectedGeneral(General selectedGeneral) {
        this.selectedGeneral = selectedGeneral;
    }

    public String getTxtBusDominio() {
        return txtBusDominio;
    }

    public void setTxtBusDominio(String txtBusDominio) {
        this.txtBusDominio = txtBusDominio;
    }

    public boolean isEsNuevo() {
        return esNuevo;
    }

    public void setEsNuevo(boolean esNuevo) {
        this.esNuevo = esNuevo;
    }

    public List<String> getLstGeneralDominio() {
        return lstGeneralDominio;
    }

    public void setLstGeneralDominio(List<String> lstGeneralDominio) {
        this.lstGeneralDominio = lstGeneralDominio;
    }

    public String getSelectedGeneralDominio() {
        return selectedGeneralDominio;
    }

    public void setSelectedGeneralDominio(String selectedGeneralDominio) {
        this.selectedGeneralDominio = selectedGeneralDominio;
    }
}